package com.leizhi.demo.onvif.utils;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Description jackson 转换工具类
 * @Version 1.0.0-SNAPSHOT
 * @Author leizhi
 * @Date 2025/6/21 15:16
 */
public class JacksonUtil {

    private final static ObjectMapper MAPPER = SingletonObjectMapperUtil.getInstance();


    /* 实体类 =》 Json */
    public static String toJson(Object obj) {
        try {
            return MAPPER.writeValueAsString(obj);
        } catch (IOException e) {
            throw new RuntimeException("类型转换错误：" + e.getMessage());
        }
    }

    /* Json =》 实体类 */
    public static <T> T toObj(String json, Class<T> clz) {
        try {
            return MAPPER.readValue(json, clz);
        } catch (IOException e) {
            throw new RuntimeException("类型转换错误：" + e.getMessage());
        }
    }

    /* Map =》 实体类 */
    public static <T> T toObject(Object mapObj, Class<T> type) {
        return MAPPER.convertValue(mapObj, type);
    }

}
